package com.xinjian.jianba.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xinjian.jianba.dto.ActivityFindOneDto;
import com.xinjian.jianba.dto.ActivityPersonIndexVo;
import com.xinjian.jianba.dto.ActivityWorkDto;
import com.xinjian.jianba.dto.AllRankDto;
import com.xinjian.jianba.dto.AllRankVo;
import com.xinjian.jianba.dto.TicketsDto;
import com.xinjian.jianba.dto.UserActivityDto;
import com.xinjian.jianba.enity.AcwardsEntity;
import com.xinjian.jianba.enity.AllRankEntity;
import com.xinjian.jianba.exception.ResponseCode;
import com.xinjian.jianba.service.TicketsService;
import com.xinjian.jianba.service.ValidatorService;

@RestController
@RequestMapping(value = "/api/v3/jianba/tickets")
public class UserTicketsController extends AbstractController{
	private static final String separator=File.separator;
	@Autowired
	private TicketsService ticketsService;
	@Autowired
	private ValidatorService validatorService;

	/**
	 * 参加比赛,保存个人资料
	 * @param userActivityDto
	 * @return
	 * @throws IOException 
	 */
	@PostMapping("/join")
	public String Join(@RequestBody UserActivityDto userActivityDto) throws IOException{
		try {
			validatorService.valid(userActivityDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		Long  activityUid=ticketsService.join(userActivityDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG,activityUid);
		
	}
	
	
	
	/**
	 * 投票接口
	 * @param ticketsDto
	 * @return
	 */
	@PostMapping("/vote")
	public String vote(@RequestBody TicketsDto ticketsDto){
		try {
			validatorService.valid(ticketsDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, ResponseCode.PARAM_ERROR_MSG);
		}
		
		int reuslt=ticketsService.vote(ticketsDto);
		if(reuslt==1){
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
		}else if(reuslt==2){
			return this.responseClientJsonData(ResponseCode.ERROR, "非注册用户,一个作品当天最多可投1票");
		}else if(reuslt==3){
			return this.responseClientJsonData(ResponseCode.ERROR, "一个作品当天最多可投3票");
		}else{
			return this.responseClientJsonData(ResponseCode.ERROR, ResponseCode.ERROR_MSG);
		}
	}
	/**
	 * 编辑个人资料接口
	 * @param userActivityDto
	 * @return
	 * @throws IOException 
	 */
	@PostMapping("/editor")
	public String editor(@RequestBody UserActivityDto userActivityDto) throws IOException{
		try {
			validatorService.valid(userActivityDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		int re=ticketsService.editor(userActivityDto);
		if(re==1){
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
		}else{
			return this.responseClientJsonData(ResponseCode.ERROR, "更新失败");
		}
	}
	/**
	 * 视频上传
	 * @param activityWorkDto
	 * @return
	 */
	@PostMapping("/workUp")
	public String workUp(@RequestBody ActivityWorkDto activityWorkDto){
		try {
			validatorService.valid(activityWorkDto);
		} catch (Exception e) {
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST, e.getMessage());
		}
		int re=ticketsService.workUp(activityWorkDto);
		if(re==1){
			return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG);
		}else{
			return this.responseClientJsonData(ResponseCode.ERROR, "上传失败");
		}
	}
	/**
	 * 个人主页查询接口
	 * @param map
	 * @return
	 */
	@PostMapping("/personIndex")
	public String personIndex(@RequestBody Map<String, String> map){
		String registerId=map.get("registerId");
		if(StringUtils.isBlank(registerId)){
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST,"用户注册id不能为空");
		}
		ActivityPersonIndexVo activityPersonIndexVo=ticketsService.getPersonIndex(registerId);
	    return this.responseClientJsonData(ResponseCode.SUCCESS, ResponseCode.SUCCESS_MSG,activityPersonIndexVo);
	}
	
	/**
	 * 排行版
	 * @param allRankDto
	 * @return
	 */
	@PostMapping("/allRank")
	public String allRank(@RequestBody AllRankDto allRankDto){
		AllRankVo allRankVo=ticketsService.allRank(allRankDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS,ResponseCode.SUCCESS_MSG,allRankVo);
	}
	/**
	 * 按编号或者姓名查询
	 * @param activityFindOneDto
	 * @return
	 */
	@PostMapping("/findOne")
	public String findOne(@RequestBody ActivityFindOneDto activityFindOneDto){
		if(StringUtils.isBlank(activityFindOneDto.getActivityNameOrActivityUid())){
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST,"请输入名字或编号");
		}
		AllRankEntity allRankEntity=ticketsService.findOne(activityFindOneDto);
		return this.responseClientJsonData(ResponseCode.SUCCESS,ResponseCode.SUCCESS_MSG,allRankEntity); 
	}
	@PostMapping("/delWork")
	public String delWork(@RequestBody Map<String, String> map){
		String workId=map.get("workId");
		if(StringUtils.isBlank(workId)){
			return this.responseClientJsonData(ResponseCode.BAD_REQUEST,"作品id不能为空");
		}
		int result=ticketsService.delWork(Long.parseLong(workId));
		if(result==1){
			return this.responseClientJsonData(ResponseCode.SUCCESS,ResponseCode.SUCCESS_MSG); 
		}else if(result==0){
			return this.responseClientJsonData(ResponseCode.SUCCESS,"该作品不存在"); 
		}else{
			return this.responseClientJsonData(ResponseCode.ERROR,"操作失败"); 

		}
	}
	@PostMapping("/award")
	public String AcwardsEntity(){
		List<AcwardsEntity> list=ticketsService.award();
		return this.responseClientJsonData(ResponseCode.SUCCESS,ResponseCode.SUCCESS_MSG,list);
	}
	@GetMapping("/downApp")
	public ResponseEntity<InputStreamResource> downApp()
            throws IOException {
        String filePath = separator+"opt"+separator+"xinjian"+separator + "app-release.apk";
        FileSystemResource file = new FileSystemResource(filePath);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getFilename()));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.contentLength())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()));
    }
}
